package com.lzx.iteam.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.CloudGroup;
import io.rong.imageloader.core.DisplayImageOptions;
import io.rong.imageloader.core.ImageLoader;
import io.rong.imageloader.core.listener.ImageLoadingListener;
import io.rong.imageloader.core.assist.ImageScaleType;
import io.rong.imageloader.core.listener.SimpleImageLoadingListener;
import io.rong.imageloader.core.display.FadeInBitmapDisplayer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class QrGroupAdapter extends BaseAdapter {

    public static final int ITEM_TYPE_NOTICE = 101;
    public static final int ITEM_TYPE_CLOUD_GROUP_ITEM = 103;


    private ArrayList<CloudGroup> mCloudGroupList = new ArrayList<CloudGroup>();
    private boolean mShowCloudGroupNoticeItem = false;
    private String mCloudGroupNoticeItemText = null;
    private LayoutInflater mLayoutInflater;
    private Activity mContext;
    private int isSelect = -1;
    private DisplayImageOptions options;
    private ImageLoadingListener animateFirstListener;
    protected ImageLoader imageLoader;

    public QrGroupAdapter(Activity c, ImageLoader imageLoader) {
        mContext = c;
        mLayoutInflater = LayoutInflater.from(c);
        options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.default_cloud_image)
                .showImageForEmptyUri(R.drawable.default_cloud_image)
                .showImageOnFail(R.drawable.default_cloud_image)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        animateFirstListener = new AnimateFirstDisplayListener();
        this.imageLoader = imageLoader;
    }

    private static class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                                      Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    //FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

    public ArrayList<CloudGroup> getCloudGroupList() {
        return mCloudGroupList;
    }


    public void updateCloudGroupNoticeItem(String text) {
        mCloudGroupList.clear();
        mShowCloudGroupNoticeItem = true;
        mCloudGroupNoticeItemText = text;
        notifyDataSetChanged();
    }

    public void updateCloudGroupList(ArrayList<CloudGroup> cloudGroupList) {
        mCloudGroupList.clear();
        mCloudGroupList.addAll(cloudGroupList);
//        mCloudGroupList = cloudGroupList;
        mShowCloudGroupNoticeItem = false;
        notifyDataSetChanged();
    }

    public void clearCloudGroupList() {
        mCloudGroupList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (mShowCloudGroupNoticeItem) {
            return ITEM_TYPE_NOTICE;
        } else {
            return ITEM_TYPE_CLOUD_GROUP_ITEM;
        }
    }

    @Override
    public int getCount() {
        if (mShowCloudGroupNoticeItem) {
            return 1;
        }
        return (mCloudGroupList != null ? mCloudGroupList.size() : 0);
    }

    @Override
    public CloudGroup getItem(int position) {
        if (ITEM_TYPE_CLOUD_GROUP_ITEM == getItemViewType(position)) {
            return mCloudGroupList != null ? mCloudGroupList.get(position) : null;
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (ITEM_TYPE_CLOUD_GROUP_ITEM == getItemViewType(position)) {
            ViewHodler hodler = null;
            final CloudGroup groupItem = getItem(position);
            if (convertView == null || convertView.getTag() == null) {
                hodler = new ViewHodler();
                convertView = mLayoutInflater.inflate(R.layout.qr_group_list_item, null);
                hodler.groupType = (ImageView) convertView.findViewById(R.id.group_type);
                hodler.groupName = (TextView) convertView.findViewById(R.id.group_name);
                hodler.groupUseMsg = (TextView)convertView.findViewById(R.id.group_use_msg);
                hodler.groupSelect = (ImageView) convertView.findViewById(R.id.cloud_group_select);
                hodler.mask = convertView.findViewById(R.id.qrcode_mask);
                convertView.setTag(hodler);
            } else {
                hodler = (ViewHodler) convertView.getTag();
            }


            String imageUrl = groupItem.groupImg;
            imageLoader.displayImage(imageUrl, hodler.groupType, options, animateFirstListener);
            hodler.groupName.setText(groupItem.groupName);
            hodler.groupUseMsg.setText(groupItem.contactCount+"个联系人 ("+groupItem.memberCount+"人在用)");
            if(position == isSelect){
                hodler.groupSelect.setVisibility(View.VISIBLE);
            }else{
                hodler.groupSelect.setVisibility(View.GONE);
            }
            if (groupItem.groupType == 0){
                if ("2".equals(groupItem.userType) || "3".equals(groupItem.userType) ){
                    hodler.mask.setVisibility(View.GONE);
                }else{
                    hodler.mask.setVisibility(View.VISIBLE);
                }
            }else{
                hodler.mask.setVisibility(View.GONE);
            }

            return convertView;
        } else {
            return bindCloudGroupNoticeItem(parent);
        }
    }

    public void setSelect(int select){
        isSelect = select;
        notifyDataSetChanged();
    }

    static class ViewHodler {
        ImageView groupType;
        TextView groupName;
        TextView groupUseMsg;
        ImageView groupSelect;
        View mask;
    }
    

    private View bindCloudGroupNoticeItem(ViewGroup parent) {
        View convertView = mLayoutInflater.inflate(R.layout.title, parent, false);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        title.setText(mCloudGroupNoticeItemText);
        title.setGravity(Gravity.CENTER);
        title.setBackgroundResource(R.color.white);
        return convertView;
    }

}